package com.sunfield.windpower.activity;

import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.sunfield.baseframe.base.BaseActivity;
import com.sunfield.baseframe.http.HttpParams;
import com.sunfield.baseframe.http.HttpResult;
import com.sunfield.baseframe.util.JsonUtil;
import com.sunfield.baseframe.util.MLog;
import com.sunfield.windpower.R;
import com.sunfield.windpower.bean.UserInfo;
import com.sunfield.windpower.http.HttpClient;
import com.sunfield.windpower.http.HttpHeadProviderImpl;
import com.sunfield.windpower.util.Tools;
import com.sunfield.windpower.util.UserUtil;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

/**
 * Created by DELL on 2017/2/6.
 */
@EActivity(R.layout.activity_web)
public class LoginActivity extends BaseActivity {

    public static final String LOGIN_URL = "https://sso.pan.cnegroup.com:8443/login?service=cneapp://login";

    @ViewById
    WebView webView;
    @ViewById
    WebView wv_hide;

    String loginUrl;

    @Bean
    HttpClient http;

    @AfterViews
    void init(){
        ll_head_bg.setVisibility(View.VISIBLE);
        ll_head_bg.setBackgroundResource(R.color.white);
        for(int i=0;i<ll_head_bg.getChildCount();i++){
            ll_head_bg.getChildAt(i).setVisibility(View.INVISIBLE);
        }

        wv_hide.getSettings().setJavaScriptEnabled(true);
        wv_hide.setWebViewClient(new WebViewClient(){
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                getUserInfo();
            }
        });

        //YunTest1
        //fang@cne
        if (webView != null) {
            webView.getSettings().setJavaScriptEnabled(true);

            webView.setWebViewClient(new WebViewClient(){
                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    MLog.d("url", "url = " + url);
                    if(url.contains("cneapp://")){
                        loginUrl = url.substring(url.indexOf("cneapp://"));

                        String cookieUrl = new HttpHeadProviderImpl().getHttpBaseUrl() + HttpClient.URL_GET_DOOR_LIST;
                        if(Tools.getCookie(cookieUrl) == null && !HttpParams.mockResult){
                            wv_hide.loadUrl(cookieUrl);
                            http.show();
                        }else{
                            getUserInfo();
                        }
                    }else {
                        webView.loadUrl(url);
                    }
                    return true;
                }
            });

            webView.loadUrl(LOGIN_URL);
//            webView.loadUrl("file:///android_asset/jump.html");
        }
    }

    private void getUserInfo(){
        http.getUserInfo(Uri.parse(loginUrl).getQueryParameter("ticket"));
    }

    @Override
    public void onHttpResult(HttpResult result) {
        super.onHttpResult(result);
        if(result.match(http, "getUserInfo")){
            if(result.isSuccess()){
                UserInfo userInfo = JsonUtil.GetEntity(JsonUtil.GetJsonObjByLevel(result.getJson(), "data"), UserInfo.class);
                MLog.d("httpData", JsonUtil.toJson(userInfo));
                UserUtil.login(userInfo);
                goToMain();
            }
        }
    }

    private void goToMain(){
        http.dismiss();
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(loginUrl));
        startActivity(intent);
        finish();
    }

    @Override
    protected boolean isSlidingRightBack() {
        return false;
    }

    @Click
    void bt_login(){
        MainActivity_.intent(this).start();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                finish();
            }
        }, 1000);
    }
}
